<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Admin;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Request;
use Hyperf\View\RenderInterface;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
use Exception;

class IndexController
{
    public function index(Request $request){

      
    	return view('index');
    }


    public function home(){

          if(Session::get('role_id') == 1 ) {
               $servicer = DB::table('supplyer_company')
               ->select('name')
               ->get();
           }else if(Session::get('role_id') == 2 ){
               $servicer = DB::table('supplyer_company')
               ->select('name')
               ->where('company_id',Session::get('session_id'))->get();
            }else if(Session::get('role_id') == 3 ){
               $servicer = DB::table('supplyer_company')
               ->select('name')
               ->where('company_id',Session::get('company_id'))->get();
            }

           $servicer_id = [];
           foreach ($servicer as $key=>$value){
               $servicer_id[] = $value->name;
           }

           $order = DB::table('order')
               ->select('supplyer_company','product_num','total_sell','create_time')
               ->where('driver_status','>=',1)
               ->whereIn('supplyer_company',$servicer_id)
               ->get();
            $output_num = [];
            $servicer_num = [];
            $number = 0;
            $barData = [];
            $barData['label'] ="bar";
            $bar_num = [];
            foreach ($order as $k=>$v){
                   $date = date('Ymd',(int)$v->create_time);
                   if(!isset($bar_num[$date])){
                       $bar_num[$date]['num'] = 1;
                   }else{
                       $bar_num[$date]['num']++;
                   }
            }

            foreach ($bar_num as $x=>$y){
                $barData['data'][] = [$x,$y['num']];
            }

           foreach ($order as $key=>$value){
               $number += $value->product_num;
               if(!isset($servicer_num[$value->supplyer_company])){
                    $servicer_num[$value->supplyer_company]['data'] = $value->product_num;
                    $servicer_num[$value->supplyer_company]['label'] = $value->supplyer_company;
                    $servicer_num[$value->supplyer_company]['color'] = '#'.rand(111000,999999);
               }else{
                    $servicer_num[$value->supplyer_company]['data'] += $value->product_num;
               }
           }

           foreach ($servicer_num as $key=>$value){
               $output_num[] = $value;
           }

        return view('home/index',['output_num'=>$output_num,'barData'=>$barData]);
    }





    public function home_back(){

         $prodcut = DB::table('order_product')
             ->select('create_time','price','category')
             ->get();
         $label=[];
         foreach ($prodcut as $key=>$value){
            if (!isset($label[$value->category])){
                $label[$value->category]['data'][] =[date('YmdHis',$value->create_time),$value->price];
                $label[$value->category]['label'] = $value->category;
            }else{
                $label[$value->category]['data'][] =[date('YmdHis',$value->create_time),$value->price];
            }
         }
            $label2 = [];
            foreach ($label as $k=>$v){
                $label2[] = $v;
            }

    	return view('home/home',['sys_info'=>self::get_sys_info(),'label'=>$label2]);
    }


    /**
     * 系统信息
     */
    static function get_sys_info(){
        $sys_info['os']             = PHP_OS;
        $sys_info['zlib']           = function_exists('gzclose') ? 'YES' : 'NO';//zlib
        $sys_info['safe_mode']      = (boolean) ini_get('safe_mode') ? 'YES' : 'NO';//safe_mode = Off
        $sys_info['timezone']       = function_exists("date_default_timezone_get") ? date_default_timezone_get() : "no_timezone";
        $sys_info['curl']			= function_exists('curl_init') ? 'YES' : 'NO';
        $sys_info['web_server']     = $_SERVER['SERVER_SOFTWARE'];
        $sys_info['phpv']           = phpversion();
        $sys_info['ip'] 			= GetHostByName($_SERVER['SERVER_NAME']);
        $sys_info['fileupload']     = @ini_get('file_uploads') ? ini_get('upload_max_filesize') :'unknown';
        $sys_info['max_ex_time'] 	= @ini_get("max_execution_time").'s'; //脚本最大执行时间
        $sys_info['set_time_limit'] = function_exists("set_time_limit") ? true : false;
        $sys_info['domain'] 		= $_SERVER['HTTP_HOST'];
        $sys_info['memory_limit']   = ini_get('memory_limit');
        $sys_info['version']   	    = 1;
        $mysqlinfo = db::select("SELECT VERSION() as version");
        $sys_info['mysql_version']  = $mysqlinfo[0]->version;
        if(function_exists("gd_info")){
            $gd = gd_info();
            $sys_info['gdinfo'] 	= $gd['GD Version'];
        }else {
            $sys_info['gdinfo'] 	= "未知";
        }
        $sys_info=(object)$sys_info;
        return $sys_info;
    }
}
